package com.tb.service.impl;

import com.tb.Store;
import com.tb.domain.Product;
import com.tb.dto.ProductDto;
import com.tb.feign.StoreService;
import com.tb.mapper.ProductMapper;
import com.tb.service.IProductService;
import com.tb.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ProductServiceImpl implements IProductService {

    @Autowired(required = false)
    private ProductMapper productMapper;
    @Autowired(required = false)
    private StoreService storeService;

    @Override
    @Transactional
    public AjaxResult add(ProductDto productDto) {
        try {
            // 1.保存到数据库
            Product product = new Product();
            product.setName(productDto.getName());
            product.setPrice(productDto.getPrice());
            productMapper.add(product);

            // 2.保存库存到库存表，调用feign接口
            Store store = new Store();
            store.setId(product.getId());
            store.setNumber(productDto.getNumber());
            AjaxResult add = storeService.add(store);

            if (!add.getSuccess()) {
                return add;
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "以熔断，请稍后重试！");
        }

    }
}
